package com.example.hotelsystem.utils;



import com.example.hotelsystem.pdf.Product;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

public class PdfUtil {

    public void createPDF(Document document, PdfWriter writer, List<Product> products) throws IOException {
        //Document document = new Document(PageSize.A4);
        try {
            document.addTitle("User information");
            document.addAuthor("ljc");
            document.addSubject("product sheet.");
            document.addKeywords("product.");
            document.open();
            PdfPTable table = createTable(writer,products);
            document.add(table);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
    public static PdfPTable createTable(PdfWriter writer,List<Product> products) throws IOException, DocumentException {
        PdfPTable table = new PdfPTable(5);//生成一个两列的表格
        PdfPCell cell;
        int size = 40;
        Font font = new Font(BaseFont.createFont("/usr/share/fonts/windows/SIMHEI.TTF", BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED));
//        Font font = new Font(BaseFont.createFont("C:\\Windows\\Fonts\\Arial.TTF", BaseFont.IDENTITY_H,
//                BaseFont.NOT_EMBEDDED));
        cell = new PdfPCell(new Phrase(products.get(0).getProductName(),font));
        cell.setColspan(1);//设置所占列数
        cell.setFixedHeight(20);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
        table.addCell(cell);


        Image cellimg = Image.getInstance(products.get(0).getProductCode());
        cell = new PdfPCell(cellimg,true);
        cell.setColspan(1);//设置所占列数
        table.addCell(cell);

        cell = new PdfPCell(new Phrase("用户基本信息介绍",font));
        cell.setColspan(3);//设置所占列数
        cell.setFixedHeight(size);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
        table.addCell(cell);

        for(int i = 1;i<products.size();i++) {
            cell = new PdfPCell(new Phrase(products.get(i).getProductName(),font));//内容
            cell.setFixedHeight(size);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
            cell.setColspan(1);
            table.addCell(cell);


            cell = new PdfPCell(new Phrase(products.get(i).getProductCode(),font));//产品名称
            cell.setFixedHeight(size);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
            cell.setColspan(4);//设置所占列数
            table.addCell(cell);
        }

        return table;
    }


}
